package com.dapeng.www.recycletest;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/16.
 */
public class RefreshFootAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * 上拉加载更多
     */
    public static final int PULLUP_LOAD_MORE = 0;

    /**
     *正在加载中
     */
    public static final int LOADING_MORE = 1;

    /**
     * 上拉没有更多数据
     */
    public static final int PULLUP_NOMORE_DATA = 3;

    /**
     * 下拉加载的状态
     */
    private int loading_more_status = 0;

    private LayoutInflater mLayoutInflater;
    private List<String> mTitles;

    /**
     * 普通item
     */
    private static final int TYPE_ITEM = 0;
    /**
     * 底部footitem
     */
    private static final int TYPE_FOOTER = 1;


    public int getLoading_more_status() {
        return loading_more_status;
    }

    public RefreshFootAdapter(Context context){

        mLayoutInflater =LayoutInflater.from(context);
        mTitles = new ArrayList<>();
        for (int i=0;i<4;i++){
            int index = i+1;
            mTitles.add("item=="+index);
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM){
            View itemView = mLayoutInflater.inflate(R.layout.item_recycler_layout,parent,false);

            ItemViewHolder itemViewHolder = new ItemViewHolder(itemView);
            return itemViewHolder;
        }else if (viewType == TYPE_FOOTER){
            View footView = mLayoutInflater.inflate(R.layout.recycler_load_more_layout,parent,false);

            FooterViewHolder footerViewHolder = new FooterViewHolder(footView);
            return footerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder){
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            itemViewHolder.item_tv.setText(mTitles.get(position)+"");
            itemViewHolder.item_tv.setTag(position);
        }else if (holder instanceof FooterViewHolder){
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            switch (loading_more_status){
                case PULLUP_LOAD_MORE:
                    footerViewHolder.foot_view_item_tv.setText("上拉加载更多数据");
                    footerViewHolder.progressbar.setVisibility(View.VISIBLE);
                    break;
                case LOADING_MORE:
                    footerViewHolder.foot_view_item_tv.setText("正在加载更多数据");
                    footerViewHolder.progressbar.setVisibility(View.VISIBLE);
                    break;
                case PULLUP_NOMORE_DATA:
                    footerViewHolder.foot_view_item_tv.setText("没有更多数据");
                    footerViewHolder.progressbar.setVisibility(View.INVISIBLE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mTitles.size()+1;
    }


    @Override
    public int getItemViewType(int position) {
        if ((position+1) == getItemCount()){
            return TYPE_FOOTER;
        }else {
            return TYPE_ITEM;
        }
    }



    public static class ItemViewHolder extends RecyclerView.ViewHolder{
        public TextView item_tv;
        public ItemViewHolder(View itemView) {
            super(itemView);
            item_tv = (TextView) itemView.findViewById(R.id.item_tv);
        }
    }

    public static class FooterViewHolder extends RecyclerView.ViewHolder{
        public TextView foot_view_item_tv;
        public ProgressBar progressbar;
        public FooterViewHolder(View footerView) {
            super(footerView);
            foot_view_item_tv = (TextView) footerView.findViewById(R.id.foot_view_item_tv);
            progressbar = (ProgressBar) footerView.findViewById(R.id.progressbar);
        }
    }



    public void addItem(List<String> newDatas) {
        mTitles.clear();
        mTitles.addAll(newDatas);
        notifyDataSetChanged();
    }

    /**
     * 增加更多数据
     * @param moreTitiles
     */
    public void addMoreItems(List<String> moreTitiles){
        mTitles.addAll(moreTitiles);
        notifyDataSetChanged();
    }

    /**
     * 改变下拉加载的状态
     * @param status
     */
    public void changeMoreStatus(int status){
        loading_more_status = status;
        notifyDataSetChanged();
    }

}
